package com.alibaba.citrus.cr.mn.order.center.facade.adapter.impl;

import com.alibaba.citrus.cr.mn.order.center.facade.adapter.SalesOrgAbilityAdapter;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.dataobject.request.SalesBizOrgByBuQueryRequest;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.dataobject.response.SalesBizOrgQueryDTO;
import com.alibaba.cloud.retail.customer.center.ability.SalesOrgAbilityService;
import com.alibaba.cloud.retail.customer.center.req.AccurateOrgReq;
import com.alibaba.cloud.retail.customer.center.req.AccurateQueryOrgReq;
import com.alibaba.cloud.retail.customer.center.req.SalesOrgCodeListReq;
import com.alibaba.cloud.retail.customer.center.resp.DivisionResp;
import com.alibaba.cloud.retail.customer.center.resp.SalesOrgListResp;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.lydass.bizphin.maca.model.Result;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zhangxiaorui
 * @ClassName SalesOrgAbilityAdapter
 * @description: TODO
 * @date 2023/05/30 17:47
 * @version: 1.0
 */
@Service
public class SalesOrgAbilityAdapterImpl implements SalesOrgAbilityAdapter {
    private static final Log log = Log.getLogger(SalesOrgAbilityAdapterImpl.class);

    @DubboReference
    private SalesOrgAbilityService salesOrgAbilityService;

    @Override
    public List<SalesBizOrgQueryDTO> querySalesOrgByCodeAndBU(List<SalesBizOrgByBuQueryRequest> requestList) {

        if(CollectionUtils.isEmpty(requestList)) {
            return Lists.newArrayList();
        }
        List<AccurateOrgReq> reqList = requestList.stream().map(p->{
            AccurateOrgReq req = new AccurateOrgReq();
            req.setBuCode(p.getBuCode());
            req.setBusinessType(p.getBusinessType());
            req.setOrgCode(p.getOrgCode());
            return req;
        }).collect(Collectors.toList());

        AccurateQueryOrgReq accurateQueryOrgReq = new AccurateQueryOrgReq();
        accurateQueryOrgReq.setOrgReqList(reqList);
        Result<List<SalesOrgListResp>> listResult = salesOrgAbilityService.findAccurateOrgList(accurateQueryOrgReq);
        log.info("销售组织查询 request={}, result={}", JSONArray.toJSONString(accurateQueryOrgReq), JSONObject.toJSONString(listResult));
        if(Objects.isNull(listResult) || CollectionUtils.isEmpty(listResult.getResult())) {
            return Lists.newArrayList();
        }
        return listResult.getResult().stream().map(p->{
            SalesBizOrgQueryDTO queryDTO = new SalesBizOrgQueryDTO();
            BeanUtils.copyProperties(p, queryDTO);
            return queryDTO;
        }).collect(Collectors.toList());
    }

    @Override
    public List<SalesBizOrgQueryDTO> querySalesOrgByCode(List<String> salesOrgCodeList) {
        if (CollectionUtils.isEmpty(salesOrgCodeList)) {
            return Lists.newArrayList();
        }

        SalesOrgCodeListReq salesOrgCodeListReq = new SalesOrgCodeListReq();
        salesOrgCodeListReq.setOrgCodeList(salesOrgCodeList);
        Result<List<SalesOrgListResp>> salesOrgByCodeList = salesOrgAbilityService.findSalesOrgByCodeList(salesOrgCodeListReq);
        log.info("销售组织查询 request={}, result={}", JSONArray.toJSONString(salesOrgCodeListReq), JSONObject.toJSONString(salesOrgByCodeList));

        if (Objects.nonNull(salesOrgByCodeList) && CollectionUtils.isNotEmpty(salesOrgByCodeList.getResult())) {
            return salesOrgByCodeList.getResult().stream().map(p -> {
                SalesBizOrgQueryDTO queryDTO = new SalesBizOrgQueryDTO();
                BeanUtils.copyProperties(p, queryDTO);

                List<String> divisionCodeList = Optional.ofNullable(p.getDivisionList()).orElse(Lists.newArrayList())
                        .stream().map(DivisionResp::getDivision).collect(Collectors.toList());
                queryDTO.setDivisionCodeList(divisionCodeList);

                return queryDTO;
            }).collect(Collectors.toList());
        }
        return Lists.newArrayList();
    }
}
